﻿<%
Class ASPLog

	Private FSO
	Private LogPath
	Private TodayPath

	'初始化
	Private Sub Class_Initialize
		set FSO = Server.CreateObject("Scripting.FileSystemObject")
		LogPath = Server.MapPath("/core/log/")
		TodayPath = GetTodayPath()
		
		'Log目录不存在的时候创建目录
		CreateFolder(LogPath)
	End Sub

	'销毁对象
	Private Sub Class_Terminate
		set FSO = nothing
	End Sub

	'写入到log文件
	Public Function Write(content)
		dim f
		set f = FSO.OpenTextFile(LogPath & "/#" & TodayPath,8,True)
		f.Write(GetNow() & " " & content & Chr(13) & Chr(10))
		f.close
		set f = nothing
	End Function

	'创建文件夹
	Public Function CreateFolder(path)
		dim f
		if not FolderExists(path) then
			set f = FSO.CreateFolder(path)
			set f = nothing
		end if
	End Function

	'判断文件夹是否存在
	Public Function FolderExists(path)
		If (FSO.FolderExists(path)) Then
			FolderExists = true
		Else
			FolderExists = false
		End If
	End Function

	'获取以今天的日期的文件名
	Private Function GetTodayPath()
		dim yy,mm,dd
		yy = right(year(now),2)
		mm = month(now)
		dd = day(now)
		if len(mm) < 2 then mm = "0" & mm
		if len(dd) < 2 then dd = "0" & dd
		GetTodayPath = yy & mm & dd & ".txt"
	end Function
	
	Private Function GetNow()
		dim n,yy,mm,dd,hh,mm2,ss
		n = now
		yy = year(n)
		mm = month(n)
		dd = day(n)
		hh = hour(n)
		mm2 = minute(n)
		ss = second(n)
		if len(mm) < 2 then mm = "0" & mm
		if len(dd) < 2 then dd = "0" & dd
		if len(hh) < 2 then hh = "0" & hh
		if len(mm2) < 2 then mm2 = "0" & mm2
		if len(ss) < 2 then ss = "0" & ss
		GetNow = yy & "-" & mm & "-" & dd & " " & hh & ":" & mm2 & ":" & ss
	end Function
End Class

dim lg
set lg = new ASPLog
%>
